package com.ydj.framework.utils.generator;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

class GeneratorUtils {
    /**
     * 读取控制台内容
     */
    public static String scanner(String tip) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + tip + "：");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    static File findProjectDir() throws IOException {
        Resource resource = new ClassPathResource("");
        File dir = resource.getFile();
        while (!dir.getName().endsWith("framework")) {
            dir = dir.getParentFile();
        }
        return dir.getParentFile();
    }

    // 把名字改成小写+下划线
    static String toSnakeCase(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }

        StringBuilder result = new StringBuilder();
        result.append(Character.toLowerCase(str.charAt(0)));

        for (int i = 1; i < str.length(); i++) {
            char c = str.charAt(i);
            if (Character.isUpperCase(c)) {
                // 如果当前字符是大写字母，则在它前面添加下划线，并转换为小写
                result.append('_').append(Character.toLowerCase(c));
            } else {
                // 否则直接添加字符
                result.append(c);
            }
        }

        return result.toString();
    }

    /**
     * 将字符串的首字母转换为大写
     * @param input 原始字符串
     * @return 首字母大写的字符串
     */
    static String uppercaseFirstLetter(String input) {
        if (input == null || input.isEmpty()) {
            return input;
        }
        return input.substring(0, 1).toUpperCase() + input.substring(1);
    }

    /**
     * 将字符串的首字母转换为小写
     * @param input 原始字符串
     * @return 首字母小写的字符串
     */
    static String lowercaseFirstLetter(String input) {
        if (input == null || input.isEmpty()) {
            return input;
        }
        return input.substring(0, 1).toLowerCase() + input.substring(1);
    }

    static void log(String msg) {
        System.out.println(msg);
    }
}
